# main.py （新增文件）
import os
import asyncio
import time
import threading
from dotenv import load_dotenv
from get_bili_danmu import DanmuMonitor
from ai_processor import AIGenerator
from tts_engine import VoiceOutput

load_dotenv()

def main():
    monitor = DanmuMonitor(os.getenv("BILI_ROOM_ID"))
    ai = AIGenerator(engine='volc')  # 可切换gpt/volc
    tts = VoiceOutput(os.getenv("VOICE_NAME"))

    while True:
        if danmu := monitor.get_new_danmu():
            # 测试
            # danmu['text'] = "你好"
            print(f"[收到弹幕] {danmu['user']}: {danmu['text']}")
            # 读取违禁词
            prohibited_words = os.getenv("PROHIBITED_WORDS").split(",")
            # 是否回复
            is_reply = True
            # 判断每一个prohibited_words中的词是否在danmu['text']中
            # 如果有违禁词，则不回复
            for word in prohibited_words:
                if word in danmu['text']:
                    # 检测到违禁词
                    print(f"[违禁词] {danmu['user']}: {danmu['text']}")
                    is_reply = False
                    break
                # else:
                #     is_reply = True
            if is_reply:
                reply = "语音助手回复弹幕：" + ai.generate_reply(danmu['text'])
                print(f"[AI回复] {reply}")
                threading.Thread(
                    target= tts.text_to_speech, 
                    args=(reply,)
                ).start()
        time.sleep(6)  # 避免频繁请求，每6秒请求一次

if __name__ == "__main__":
    asyncio.run(main())  # 用asyncio运行main函数